package com.intel.factorytest.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Button;

import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.io.DataOutputStream;
import java.io.DataInputStream;

import com.intel.factorytest.R;

import com.intel.factorytest.TestFileOp;
import com.intel.factorytest.TestShell;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.ShellUtilsPlus;

public class LEDTest_back extends BaseActivity {

    private final String TAG = "LEDTest_back"; 

    String RED_LED_DEV = "/sys/class/leds/red/brightness";
    String GREEN_LED_DEV = "/sys/class/leds/green/brightness";
    String BLUE_LED_DEV = "/sys/class/leds/blue/brightness";

    private List<String> redList;
    private List<String> greenList;
    private List<String> blueList;
    private Button mRedButton;
    private Button mGreenButton;
    private Button mBlueButton;
    ShellUtilsPlus.CommandResult mCmdResult;
    private String cmdSuccessMsg;
    private String cmdErrorMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.led_test);
        initView();
        initEvent();
        redList = new ArrayList<>();
        initData(RED_LED_DEV, redList);
        Log.i(TAG, "redList = " + redList) ;
        /*
        greenList = new ArrayList<>();
        initData(2, greenList);
        blueList = new ArrayList<>();
        initData(3, blueList);
        */
        initEvent();
    }

    private void initEvent() {
        
        mRedButton = (Button) findViewById(R.id.bt_red);
        mGreenButton = (Button) findViewById(R.id.bt_green);
        mBlueButton = (Button) findViewById(R.id.bt_blue);

        mRedButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.setClickable(false);
                Thread playThread = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        for (int i = 0; i < redList.size(); i++) {
                            Log.i(TAG,"red:" + redList.get(i));
                            mCmdResult = ShellUtilsPlus.execCommand(redList.get(i),false);
                            cmdSuccessMsg = mCmdResult.successMsg;
                            cmdErrorMsg = mCmdResult.errorMsg;
                            LogUtils.logi( "LED Test Red get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
                        }
                    }
                };
                playThread.start();
            }
        });

        mGreenButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                v.setClickable(false);
                Thread playThread = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        for (int i = 0; i < greenList.size(); i++) {
                            Log.i(TAG,"green:" + greenList.get(i));
                            callShellCommand(greenList.get(i), 0);
                        }
                    }
                };
                playThread.start();
            }
        });

        mBlueButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                v.setClickable(false);
                Thread playThread = new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        for (int i = 0; i < blueList.size(); i++) {
                            Log.i(TAG, "blue : " + blueList.get(i));
                            callShellCommand(blueList.get(i), 0);
                        }
                    }
                };
                playThread.start();
            }
        });

    }

    private void initData(String led_device, List<String> list) {
        //echo  light_level > led_device ("/sys/class/leds/red/brightness")
        StringBuffer buffer = new StringBuffer();
        int light_level = 100;
        while (light_level <= 255) {
            buffer.append("echo ");
            buffer.append(String.valueOf(light_level));
            buffer.append(" > ");
            buffer.append(led_device);
            list.add(buffer.toString());
            light_level = light_level + 20;
            buffer.setLength(0);
        }
    }

/*
    private void initData(int startId, List<String> list) {
        //echo  led_id   light_level > /sys/class/leds/aw9817_led/led_control
        StringBuffer buffer = new StringBuffer();
        while (startId <= 48) {
            buffer.append("echo ");
            buffer.append(String.valueOf(startId));
            buffer.append(" 63 > /sys/class/leds/aw9817_led/led_control");
            list.add(buffer.toString());
            startId = startId + 3;
            buffer.setLength(0);
        }
    }
*/
    private void callShellCommand(String cmd, int sleep) {
        try {
            Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", cmd});
            //Process p = Runtime.getRuntime().exec(cmd);
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String tmp = "";
            while ((tmp = br.readLine()) != null) {
                Log.i(TAG, "tmp = " + tmp);
            }
            br.close();

            InputStream eis = p.getErrorStream();
            InputStreamReader eisr = new InputStreamReader(eis);
            BufferedReader ebr = new BufferedReader(eisr);

            while ((tmp = ebr.readLine()) != null) {
                Log.i(TAG, "tmp2 = " + tmp);
            }
            ebr.close();
            Thread.sleep(sleep);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        LogUtils.loge("onDestroy");
        super.onDestroy();
    }

    @Override
    public void finish() {
        LogUtils.loge("finish is_pass = " + is_pass);
        tc.setTestResult(is_pass);
        super.finish();
    }

}

